﻿using System;
using System.Text;
using System.Data;
using System.Data.OracleClient;
using DBUtility;

namespace OracleDAL.Common
{
    /// <summary>
    /// 数据访问类ResourceStatus。
    /// </summary>
    public class ResourceStatus
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public ResourceStatus()
        { }
        #region  成员方法
        /// <summary>
        /// 获取资源状态
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="RESOURCEID">资源ID</param>
        /// <returns></returns>
        public Model.Common.ResourceStatus GetModel(string SUBSYSTEMID, string RESOURCETYPEID, string RESOURCEID)
        {

            StringBuilder strSql = new StringBuilder();
            //有需要将该SQL语句优化修改成视图
            strSql.Append("select SUBSYSTEMID,RESOURCETYPEID,RESOURCETYPENAME,RESOURCETABLE ");
            strSql.Append(" from RESOURCETYPE ");
            strSql.Append(" where SUBSYSTEMID=:SUBSYSTEMID and RESOURCETYPEID=:RESOURCETYPEID ");
            OracleParameter[] parameters = {
                    new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
                                           new OracleParameter(":RESOURCETYPEID", OracleType.VarChar,25)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = RESOURCETYPEID;

            Model.Common.ResourceStatus model = new Model.Common.ResourceStatus();
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                model.RESOURCETYPEID = ds.Tables[0].Rows[0]["RESOURCETYPEID"].ToString();
                model.RESOURCETYPENAME = ds.Tables[0].Rows[0]["RESOURCETYPENAME"].ToString();
                model.SUBSYSTEMID = ds.Tables[0].Rows[0]["SUBSYSTEMID"].ToString();
                strSql.Remove(0, strSql.Length);//清除strSql
                strSql.Append("select RESOURCEID,RESOURCENAME,RESOURCESTATUS ");
                strSql.Append(" from  ");
                strSql.Append(ds.Tables[0].Rows[0]["RESOURCETABLE"].ToString());
                strSql.Append(" where RESOURCEID=:RESOURCEID ");
                OracleParameter[] ps = { new OracleParameter(":RESOURCEID", OracleType.VarChar, 25) };
                ps[0].Value = RESOURCEID;

                DataSet ds1 = OracleHelper.Query(strSql.ToString(), ps);
                if (ds1.Tables[0].Rows.Count > 0)
                {
                    model.RESOURCEID = ds1.Tables[0].Rows[0]["RESOURCEID"].ToString();
                    model.RESOURCENAME = ds1.Tables[0].Rows[0]["RESOURCENAME"].ToString();
                    model.RESOURCESTATUS = ds1.Tables[0].Rows[0]["RESOURCESTATUS"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 获取资源状态数据集
        /// </summary>
        /// <param name="SUBSYSTEMID">子系统ID</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="TYPE">资源类型</param>
        /// <returns></returns>
        public DataSet GetList(string SUBSYSTEMID, string RESOURCETYPEID, string TYPE)
        {

            StringBuilder strSql = new StringBuilder();
            //有需要将该SQL语句优化修改成视图
            strSql.Append("select RT.SUBSYSTEMID,RT.RESOURCETYPEID,RT.RESOURCETYPENAME,RB.RESOURCEID,RB.RESOURCENAME,CMAS.ENTITYSTATUS AS RESOURCESTATUS ");
            strSql.Append(" from RESOURCETYPE RT,RESOURCEBASE RB,COM_MGR_ADP_STATUS CMAS");
            strSql.Append(" where RT.SUBSYSTEMID=:SUBSYSTEMID and RT.RESOURCETYPEID=:RESOURCETYPEID and RT.TYPE=:TYPE and RT.RESOURCETYPEID=RB.RESOURCETYPEID and ");
            strSql.Append(" RB.RESOURCENAME=CMAS.ENTITYNAME and RB.RESOURCETYPEID=CMAS.ENTITYTYPE ");
            OracleParameter[] parameters = {
                    new OracleParameter(":SUBSYSTEMID", OracleType.VarChar,25),
                                           new OracleParameter(":RESOURCETYPEID", OracleType.VarChar,25),
                                           new OracleParameter(":TYPE", OracleType.VarChar,20)};
            parameters[0].Value = SUBSYSTEMID;
            parameters[1].Value = RESOURCETYPEID;
            parameters[2].Value = TYPE;
            return OracleHelper.Query(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RT.SUBSYSTEMID,RT.RESOURCETYPEID,RT.RESOURCETYPENAME,RB.RESOURCEID,RB.RESOURCENAME,CMAS.ENTITYSTATUS AS RESOURCESTATUS ");
            strSql.Append(" from RESOURCETYPE RT,RESOURCEBASE RB,COM_MGR_ADP_STATUS CMAS ");
            strSql.Append(" where RT.RESOURCETYPEID=RB.RESOURCETYPEID and  RB.RESOURCENAME=CMAS.ENTITYNAME  and RB.RESOURCETYPEID=CMAS.ENTITYTYPE ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" and " + strWhere);
            }
            strSql.Append(" order by RT.SUBSYSTEMID,RT.RESOURCETYPEID,RB.RESOURCEID");
            return OracleHelper.Query(strSql.ToString());
        }
        /// <summary>
        /// 删除逻辑设备
        /// </summary>
        /// <param name="LOGICEQUIPMENTID">逻辑设备ID</param>
        /// <returns></returns>
        public int Delete(string LOGICEQUIPMENTID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete ROOM_EQUIPMENTSUPSUB where LOGICEQUIPMENTID=:LOGICEQUIPMENTID; ");
            strSql.Append("delete ROOM_EQUIPMENTLAYOUT where LOGICEQUIPMENTID=:LOGICEQUIPMENTID; ");
            OracleParameter[] parameters = {
                    new OracleParameter(":LOGICEQUIPMENTID", OracleType.VarChar,25)};
            parameters[0].Value = LOGICEQUIPMENTID;
            return OracleHelper.ExecuteSql(strSql.ToString(), parameters);
        }
        #endregion  成员方法

        #region 获取设备属性字段
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select RT.TYPEID,RT.TYPENAME,RT.TYPECNNAME,CRI.QUERYFROMVIEW ,CRF.ENFIELD,CRF.CHFIELD,CRF.DATATYPE,CRF.TRANSTABLE,CRF.TRANSCOL,CRF.TRANSEDCOL ");
            strSql.Append(" from RESOURCETYPE RT,CFG_RESOURCE_INFO CRI,CFG_RESOURCE_FIELDS CRF ");
            strSql.Append(" where CRF.TABLENAME=CRI.QUERYFROMVIEW and RT.TABLENAME=CRI.RESID and RT.TYPEID=:TYPEID order by SERIAL");
            OracleParameter[] parameters = {
                    new OracleParameter(":TYPEID", OracleType.VarChar,25)};
            parameters[0].Value = TYPEID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取类型属性字段
        /// </summary>
        /// <param name="TABLENAME">资源对应的表明</param>
        /// <param name="ISVISIBLE">字段是否显示(为空显示所有,N查询不显示,Y查询显示字段)</param>
        /// <returns></returns>
        public DataSet GetColumnesList(string TABLENAME, string ISVISIBLE)
        {
            StringBuilder strSql = new StringBuilder();
            //strSql.Append(" select RP.TYPEID,RP.TYPENAME,RP.TYPECNNAME,CRI.QUERYFROMVIEW ,CRF.ENFIELD,CRF.CHFIELD,CRF.DATATYPE,CRF.TRANSTABLE,CRF.TRANSCOL,CRF.TRANSEDCOL ");
            //strSql.Append(" from V_RESOURCE_PER RP,CFG_RESOURCE_INFO CRI,CFG_RESOURCE_FIELDS CRF ");
            //strSql.Append(" where ");
            //if (ISVISIBLE.Length > 0)
            //{
            //    strSql.Append("  CRF.ISVISIBLE='" + ISVISIBLE + "' and ");
            //}
            //strSql.Append("  CRF.TABLENAME=CRI.MODIFYINFOTABLE and RP.TABLENAME=CRI.RESID and RP.TABLENAME=:TABLENAME  ");
            //strSql.Append("  order by TO_NUMBER(SERIAL)");
            strSql.Append(" select CRI.QUERYFROMVIEW ,CRF.ENFIELD,CRF.CHFIELD,CRF.DATATYPE,CRF.TRANSTABLE,CRF.TRANSCOL,CRF.TRANSEDCOL ");
            strSql.Append(" from CFG_RESOURCE_INFO CRI,CFG_RESOURCE_FIELDS CRF ");
            strSql.Append(" where ");
            if (ISVISIBLE.Length > 0)
            {
                strSql.Append("  CRF.ISVISIBLE='" + ISVISIBLE + "' and ");
            }
            strSql.Append("  CRF.TABLENAME=CRI.MODIFYINFOTABLE and CRI.RESID=:TABLENAME  ");
            strSql.Append("  order by TO_NUMBER(SERIAL)");

            OracleParameter[] parameters = {
                    new OracleParameter(":TABLENAME", OracleType.VarChar,100)};
            parameters[0].Value = TABLENAME;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取资源页签
        /// </summary>
        /// <param name="TYPEID">类型ID</param>
        /// <returns></returns>
        public DataSet GetResourceTab(string TYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select TYPEID,NETWORKID,TYPENAME,TYPECNNAME,TABLENAME,RTYPE from V_RESOURCE_PER ");
            strSql.Append(" where TYPEID=:TYPEID order by RTYPE desc");
            OracleParameter[] parameters = {
                    new OracleParameter(":TYPEID", OracleType.VarChar,25)};
            parameters[0].Value = TYPEID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取资源值(属性或性能)
        /// </summary>
        /// <param name="TABLENAME">查询资源表名</param>
        /// <param name="RESOURCETYPEID">查询资源ID(如果资源类型ID没有，就直接使用表明，如果资源类型ID存在就查询相应的视图)</param>
        /// <param name="SORT">排序字段</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetResourceValue(string TABLENAME, string RESOURCETYPEID, string TYPE, string SORT, string strWhere)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_tablename", OracleType.VarChar,100),
                    new OracleParameter("p_resourcetypeid", OracleType.VarChar,100),
					new OracleParameter("p_strwhere", OracleType.VarChar,400),
                    new OracleParameter("p_type", OracleType.VarChar,400),
                    new OracleParameter("p_sort", OracleType.VarChar,400),
                    new OracleParameter("v_cursor", OracleType.Cursor)};
            parameters[0].Value = TABLENAME;
            parameters[1].Value = RESOURCETYPEID;
            parameters[2].Value = strWhere;
            parameters[3].Value = TYPE;
            parameters[4].Value = SORT;

            parameters[0].Direction = ParameterDirection.Input;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[3].Direction = ParameterDirection.Input;
            parameters[4].Direction = ParameterDirection.Input;
            parameters[5].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GET_PER_VALUE", parameters, "table1");
        }
        /// <summary>
        /// 获取机房的所有资源
        /// </summary>
        /// <param name="ROOMID">机房ID</param>
        /// <returns></returns>
        public DataSet GetRoomEquip(string ROOMID)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_roomid", OracleType.VarChar,25),
                    new OracleParameter("p_cursor", OracleType.Cursor)};
            parameters[0].Value = ROOMID;

            parameters[0].Direction = System.Data.ParameterDirection.Input;
            parameters[1].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GET_RESOURCE_ROOM", parameters, "table1");
        }
        /// <summary>
        /// 获取设备属性名称
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="RESOURCETYPEID">资源类型ID</param>
        /// <param name="TYPE">类型</param>
        /// <param name="SORT">排序字段</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetEquipProperty(string TABLENAME, string RESOURCETYPEID, string TYPE, string SORT, string strWhere)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_tablename", OracleType.VarChar,100),
                    new OracleParameter("p_resourcetypeid", OracleType.VarChar,100),
					new OracleParameter("p_strwhere", OracleType.VarChar,400),
                    new OracleParameter("p_type", OracleType.VarChar,400),
                    new OracleParameter("p_sort", OracleType.VarChar,400),
                    new OracleParameter("v_cursor", OracleType.Cursor)};
            parameters[0].Value = TABLENAME;
            parameters[1].Value = RESOURCETYPEID;
            parameters[2].Value = strWhere;
            parameters[3].Value = TYPE;
            parameters[4].Value = SORT;

            parameters[0].Direction = ParameterDirection.Input;
            parameters[1].Direction = ParameterDirection.Input;
            parameters[2].Direction = ParameterDirection.Input;
            parameters[3].Direction = ParameterDirection.Input;
            parameters[4].Direction = ParameterDirection.Input;
            parameters[5].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GET_RESOURCE_PROPERTY", parameters, "table1");
        }
        /// <summary>
        /// 获取LOOP设备是否在网
        /// </summary>
        /// <returns></returns>
        public DataSet GetLoopIsInnet()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  MEID,ISINNET from LOOP_AM3440_ME");
            strSql.Append(" where  ISINNET='N'");
            strSql.Append("  union ");
            strSql.Append(" select  MEID,ISINNET from LOOP_V42009_ME ");
            strSql.Append("   where  ISINNET='N' ");
            strSql.Append(" order by meid");
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="AREAID">区域ID</param>
        /// <param name="ROOMID">机房ID</param>
        /// <param name="EQUIPID">设备ID</param>
        /// <returns></returns>
        public DataSet GetEquipList(string AREAID, string ROOMID, string EQUIPID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select NE_ID,NE_CLASS,NE_TYPE,EQUIP_NAME,EQUIP_ID,BELONGROOM,CHINA_NAME,BELONGAREA,ROOMNAME,BELONGAREANAME ");
            strSql.Append(" from V_EQUIPMENT_LIST ");
            strSql.Append(" where 1=1 ");
            if (AREAID.Length > 0)
            {
                strSql.Append("  and BELONGAREA='" + AREAID + "' ");
            }
            if (ROOMID.Length > 0)
            {
                strSql.Append("  and BELONGROOM='" + ROOMID + "' ");
            }
            if (EQUIPID.Length > 0)
            {
                strSql.Append("  and EQUIP_ID='" + EQUIPID + "' ");
            }
            strSql.Append("  order by BELONGAREA,to_number(BELONGROOM),EQUIP_ID");

            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取IT设备基本指标数据
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <param name="SORT">排序</param>
        /// <returns></returns>
        public DataSet GetIndicatorValue(string TABLENAME, string strWhere, string SORT)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select ID,SAMECATEGORYNAME,sum(EFFICIENCY) as EFFICIENCY,TYPE ");
            strSql.Append(" from V_IT_PREF");
            strSql.Append(" where 1=1 ");
            if (strWhere.Length > 0)
            {
                strSql.Append("  and ID='" + strWhere + "' ");
            }
            strSql.Append("  group by ID,TYPE,SAMECATEGORYNAME  order by ");
            if (SORT.Length == 0)
            {
                SORT = "ID,TYPE";
            }
            strSql.Append(SORT);
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取设备ID对象下的设备列表
        /// </summary>
        /// <param name="EQUIPID">设备ID</param>
        /// <param name="EQUIPTYPE">设备类型</param>
        /// <returns></returns>
        public DataSet GetEquip(string EQUIPID, string EQUIPTYPE)
        {
            OracleParameter[] parameters = {
					new OracleParameter("p_equipid", OracleType.VarChar,25),
                    new OracleParameter("p_equiptype", OracleType.VarChar,200),
                    new OracleParameter("p_cursor", OracleType.Cursor)};
            parameters[0].Value = EQUIPID;
            parameters[1].Value = EQUIPTYPE;

            parameters[0].Direction = System.Data.ParameterDirection.Input;
            parameters[1].Direction = System.Data.ParameterDirection.Input;
            parameters[2].Direction = System.Data.ParameterDirection.Output;

            return OracleHelper.RunProcedure("GETEQUIPLIST", parameters, "table1");
        }
        /// <summary>
        /// 获取设备各类型的数目
        /// </summary>
        /// <param name="EQUIPID">上级设备ID</param>
        /// <returns></returns>
        public DataSet GetTypeCount(string EQUIPID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select EQUIPTYPE,COUNT(LOGICEQUIPMENTID)  as NUM ");
            strSql.Append("  from ROOM_EQUIPMENTSUPSUB ");
            strSql.Append(" where UPLOGICEQUMENTID=:UPLOGICEQUMENTID ");
            strSql.Append("  group by EQUIPTYPE ");
            OracleParameter[] parameters = {
                    new OracleParameter(":UPLOGICEQUMENTID", OracleType.VarChar,25)};
            parameters[0].Value = EQUIPID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        /// <summary>
        /// 获取资源类型的基本信息
        /// </summary>
        /// <param name="TYPEID">资源类型ID</param>
        /// <returns></returns>
        public DataSet GetResourceTypeInfo(string TYPEID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  TYPEID,TYPENAME,TYPECNNAME,TABLENAME,REMARK ,TYPE,SUBSYSTEMID,ISDISPLAY,ICO,ISPANEL ,ISPROPERTY,ISPERFORMANCE,PANELID ");
            strSql.Append("  from RESOURCETYPE ");
            strSql.Append("  where TYPEID=:TYPEID  or TYPENAME=:TYPEID");
            OracleParameter[] parameters = {
                    new OracleParameter(":TYPEID", OracleType.VarChar,25)};
            parameters[0].Value = TYPEID;
            DataSet ds = OracleHelper.Query(strSql.ToString(), parameters);
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
        #endregion
        /// <summary>
        /// 查询表
        /// </summary>
        /// <param name="TABLENAME">表名</param>
        /// <param name="strWhere">查询条件</param>
        /// <returns></returns>
        public DataSet GetResourceType(string TABLENAME, string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select  * ");
            strSql.Append("  from " +TABLENAME );
            if (strWhere.Length > 0) {
                strSql.Append("  where " + strWhere);
            }
            DataSet ds = OracleHelper.Query(strSql.ToString());
            if (ds != null)
            {
                return ds;
            }
            return null;
        }
    }
}